package com.dense.kuiniu.bookborrow.setting;


import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.base.BaseActivity;
import com.dense.kuiniu.bookborrow.dialog.PasswordInsureDialog;
import com.dense.kuiniu.bookborrow.xjSetting.listener.ScreenImageCutListener;
import com.dense.kuiniu.bookborrow.xjSetting.manager.PingBaoManager;
import com.dense.kuiniu.bookborrow.xjSetting.view.BaseView.XJImageView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 屏保界面
 */
public class ScreenImageActivity extends BaseActivity implements ScreenImageCutListener {

    private TextView hint;
    private XJImageView image;
    private List<String> photos = new ArrayList<>();
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_image);
        image = findViewById(R.id.image);
        hint = findViewById(R.id.hint);

        PingBaoManager.getInstance().setListener(this);
        //获取图片列表
        File file = PingBaoManager.getInstance().savePath();
        if (file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                String name = listFile.getName();
                name = name.toLowerCase();
                if (name.endsWith(".png")
                        || name.endsWith(".jpg")) {
                    photos.add(listFile.getAbsolutePath());
                }
            }
        } else {
            ToastUtils.showShort("图片路径为非文件夹！！");
        }

        if (photos.size() > 0) {
          //  hint.setVisibility(View.GONE);
           // showImage();
        } else {
          //  hint.setVisibility(View.VISIBLE);
//            image.setImageResource(R.mipmap.background);
        }
    }

    private void showImage() {
        if (index >= photos.size()) {
            index = 0;
        }
        if (photos.size()==0)return;
        image.loadImage(photos.get(index));

    }

    private PasswordInsureDialog insureDialog;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        PingBaoManager.getInstance().updateClickTime();
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            onBackPressed();
//            if (insureDialog==null||!insureDialog.isShowing()) {
//                insureDialog = PasswordInsureDialog.startPasswordInsure(this, 1, new EventAction() {
//                    @Override
//                    public void onAction(int ID) {
//                        onBackPressed();
//                    }
//                });
//            }
        }
        return super.dispatchTouchEvent(ev);
    }
private Handler handler=new Handler();
    @Override
    public void onCut() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                index++;
                showImage();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        PingBaoManager.getInstance().setListener(null);
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}